<template>
  <div>
    <!-- firststart -->
    <div style="margin: 40px;margin-top: -5px;margin-bottom: 20px;">
      <div style="padding: 15px;padding-left: 30px;background-color: #fdb6a7;">
          <div style="">当前订单状态：<span style="color: #0eb0fd;">{{orderDetailList.status_name}}</span></div>
          <div style="margin-top: 5px;margin-bottom: -5px;">

            <!--快递发货，只有配送方式为快递且订单状态为待发货和取消申请订单的时候才显示-->
            <el-button v-if="(orderDetailList.status == 2 || orderDetailList.status == 8) &&  orderDetailList.delivery_method =='express_delivery'" type="success" size="mini" @click="deliverGoods(orderDetailList)">发货</el-button>
            <!--骑手配送，只有配送方式为骑手配送且订单状态为待发货和取消申请订单的时候才显示-->
            <!--<el-button v-if="(orderDetailList.status == 2 || orderDetailList.status == 8) && orderDetailList.delivery_method =='instant_delivery'" type="success" size="mini" @click="riderDistribution(orderDetailList.id)">呼叫配送</el-button>-->
            <!--确认收货，只有配送方式为自提且订单状态为待发货和取消申请订单的时候才显示-->
            <el-button v-if="(orderDetailList.status == 3 || orderDetailList.status == 8) && orderDetailList.delivery_method =='self_pick'" type="success" size="mini" @click="confirmReceive(orderDetailList.id)">确认收货</el-button>
            <el-button style="margin-top: 5px" type="primary" size="mini" v-if="orderDetailList.status == 5" @click="showRefundDialog(orderDetailList,'apply')">处理申请</el-button>
            <el-button style="margin-top: 5px" type="primary" size="mini" v-else-if="!([5,6].includes(orderDetailList.status))&&orderDetailList.is_pay==1&&orderDetailList.refund_amount!=orderDetailList.pay_amount" @click="showRefundDialog(orderDetailList,'refund')">退款</el-button>
          </div>
      </div>
    </div>

    <div class="wrapBetween">
      <div class="title" style="margin-top: 20px;">
        <span>物流信息</span>
        <el-button v-if="orderTransportInfo&&orderTransportInfo.status!=99" style="margin-left: 30px;color: red" type="text" @click="cancelDeliveryOrderDialog = true">取消配送订单</el-button>
      </div>

      <!--骑手配送-->
      <div v-if="orderDetailList.delivery_method==='instant_delivery'">
        <el-row>
          <el-col :span="5">
            <div class="detailCss">
              <span class="detailSpan">配送方式</span>
              <span>{{orderDetailList.delivery_method_name}}</span>
            </div>
          </el-col>
          <el-col :span="7">
            <div class="detailCss" v-if="orderTransportInfo.mt_peisong_id">
              <span class="detailSpan" style="width: 80px">美团运单号</span>
              <span>{{orderTransportInfo.mt_peisong_id}}</span>
            </div>
          </el-col>
          <el-col :span="6" v-if="orderTransportInfo.courier_name">
            <div class="detailCss">
              <span class="detailSpan">骑手姓名</span>
              <span>{{orderTransportInfo.courier_name}}</span>
            </div>
          </el-col>
          <el-col :span="6" v-if="orderTransportInfo.courier_phone">
            <div class="detailCss">
              <span class="detailSpan">骑手电话</span>
              <span>{{orderTransportInfo.courier_phone}}</span>
            </div>
          </el-col>
        </el-row>
        <div v-if="orderTransportInfo.mt_peisong_id">
          <el-row v-if="orderTransportInfo.status==99">
            <el-col :span="5">
              <div class="detailCss">
                <span class="detailSpan" style="width: 100px">美团配送状态</span>
                <span>{{orderTransportInfo.status_name}}</span>
              </div>
            </el-col>
            <el-col :span="7">
              <div class="detailCss">
                <span class="detailSpan">取消时间</span>
                <span>{{orderTransportInfo.updated_at}}</span>
              </div>
            </el-col>
            <el-col>
              <div class="detailCss">
                <span class="detailSpan">取消原因</span>
                <span>{{orderTransportInfo.cancel_reason}}</span>
              </div>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px;" v-else>
            <el-col :span="8">
              <el-steps :active="orderTransportStep" finish-status="success">
                <el-step :title="'待调度'" description=""></el-step>
                <el-step :title="'已接单'" description=""></el-step>
                <el-step :title="'已取货'" description=""></el-step>
                <el-step :title="'已送达'" description=""></el-step>
              </el-steps>
            </el-col>
            <el-col :span="10">
              <el-button type="primary" size="mini" style="margin-left: 30px" @click="checkOrderStatus" >刷新</el-button>
              <span style="margin-left: 15px;color: grey">状态更新时间：{{orderTransportInfo.operate_time}}</span>
            </el-col>
          </el-row>
        </div>
      </div>

      <!--其他配送-->
      <div v-else>
        <div  v-if="orderDetailList.delivery_method_name !='自提'">
          <div class="detailCss">
            <span class="detailSpan">物流公司</span>
            <span v-if="orderDetailList.express!=null">{{orderDetailList.express}}</span>
            <span v-else>未填写</span>
          </div>
          <div class="detailCss">
            <span class="detailSpan">运单号</span>
            <span  class="wrapSpan" v-if="orderDetailList.express_num!=null">{{orderDetailList.express_num}}</span>
            <span v-else>未填写</span>
          </div>
        </div>
        <div class="detailCss">
          <span class="detailSpan">配送方式</span>
          <span>{{orderDetailList.delivery_method_name}}</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method == 'express_delivery' || orderDetailList.delivery_method == 'instant_delivery' ">
          <span class="detailSpan">运费</span>
          <span>{{orderDetailList.delivery_fee}}</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method_name !='自提'">
          <span class="detailSpan">发货时间</span>
          <span v-if="orderDetailList.express_time!=null">{{orderDetailList.express_time}}</span>
          <span v-else>尚未发货</span>
        </div>
        <div class="detailCss" v-if="orderDetailList.delivery_method_name =='自提'">
          <span class="detailSpan">收货时间</span>
          <span v-if="orderDetailList.receive_time!=null">{{orderDetailList.receive_time}}</span>
          <span v-else>尚未收货</span>
        </div>
      </div>

    </div>

    <div class="wrapBetween">
      <!-- 左边 -->
      <div class="">
        <div class="title">买家信息</div>
        <div class="detailCss">
          <span class="detailSpan">收货姓名</span>
          <span v-if="address_info.consignee!='' ">{{address_info.consignee}}</span>
          <span v-else>未填写</span>
        </div>
        <div class="detailCss">
          <span class="detailSpan">手机号码</span>
          <span>{{address_info.phone}}</span>
        </div>
        <div class="detailCss">
          <span class="detailSpan">收货地址</span>
          <span v-if="address_info.province!=''">
            <span>{{address_info.province}}/{{address_info.city}}/{{address_info.area}}/{{address_info.address}}</span>
          </span>
          <span v-else>
            地址未填写
          </span>
        </div>

      </div>
    </div>

    <div class="wrapBetween">
      <div class="">
        <div class="title">订单信息</div>
        <el-row>
          <el-col :span="12">
            <div class="detailCss">
              <span class="detailSpan">订单号</span>
              <span class="wrapSpan">{{orderDetailList.order_num}}</span>
            </div>
            <div class="detailCss">
              <span class="detailSpan">订单状态</span>
              <span>{{orderDetailList.status_name}}</span>
            </div>
            <div class="detailCss">
              <span class="detailSpan">所属商家</span>
              <span>{{orderDetailList.merchant_name}}</span>
            </div>
            <div class="detailCss">
              <span class="detailSpan">订单备注</span>
              <span v-if="orderDetailList.remark!=''">{{orderDetailList.remark}}</span>
              <span v-else>暂无备注</span>
            </div>
          </el-col>

          <el-col :span="12">
            <div class="detailCss" v-if="orderDetailList.status==6">
              <span class="detailSpan">取消时间</span>
              <span v-if="orderDetailList.updated_at!=null">{{orderDetailList.updated_at}}</span>
            </div>
            <div class="detailCss">
              <span class="detailSpan">创建时间</span>
              <span>{{orderDetailList.created_at}}</span>
            </div>
            <div class="detailCss">
              <span class="detailSpan">支付时间</span>
              <span v-if="orderDetailList.pay_time!=null">{{orderDetailList.pay_time}}</span>
              <span v-else>未支付</span>
            </div>
          </el-col>
        </el-row>

      </div>
    </div>

    <!--配送信息-->
    <!--<div class="secondConten" v-if="orderDetailList.delivery_method==='instant_delivery'">
      <div class="contenBottem ">
        <div class="title" style="margin-bottom: 20px;">骑手配送信息</div>
        <div class="prescription-info">
          <el-row>
            <el-col :span="12">
              <el-row>
                <el-col class="item-left" :span="8">美团内部订单id</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.mt_peisong_id}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">收货人姓名</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.receiver_name}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">收货人电话</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.receiver_phone}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">收货人地址</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.receiver_address}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">配送状态</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.status_name}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">状态更新时间</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.operate_time}}</el-col>
              </el-row>

            </el-col>
            <el-col :span="12">
              <el-row>
                <el-col class="item-left" :span="8">骑手名称</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.courier_name}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">骑手电话</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.courier_phone}}</el-col>
              </el-row>
              <el-row>
                <el-col class="item-left" :span="8">预计送达时间</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.predict_delivery_time}}</el-col>
              </el-row>
              <el-row v-if="orderTransportInfo.cancel_reason">
                <el-col class="item-left" :span="8">取消原因详情</el-col>
                <el-col class="item-right" :span="16">{{orderTransportInfo.cancel_reason}}</el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
      </div>
    </div>-->

    <!--处方信息-->
    <div class="secondConten" v-if="orderDetailList.refund_info&&orderDetailList.refund_info.length>0">
      <div class="contenBottem ">
        <div class="title" style="margin-bottom: 20px;">退款信息</div>
        <el-table :data="orderDetailList.refund_info" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                  border style="width: 100%;">
          <el-table-column align="center" prop="refund_num" label="序号" />
          <el-table-column align="center" prop="refund_amount" label="已退金额" />
          <el-table-column align="center" prop="refund_reason" label="退款原因" />
          <el-table-column align="center" prop="status" label="状态" />
        </el-table>
      </div>
    </div>

    <!--<div class="secondConten">
      <div class="contenBottem ">
        <div class="title" style="margin-bottom: 20px;">处方信息</div>
        <el-table :data="[orderDetailList.prescription_info]" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                  border style="width: 100%;">
          <el-table-column align="center" prop="user_drug_Info.patientName" label="用药人真实姓名" />
          <el-table-column align="center" prop="user_drug_Info.patientSex_name" label="性别" />
          <el-table-column align="center" prop="user_drug_Info.patientAge" label="年龄" />
          <el-table-column align="center" prop="user_drug_Info.patientWeight" label="体重" />
          <el-table-column align="center" prop="user_drug_Info.patientPhone" label="手机号" />
          <el-table-column align="center" prop="user_drug_Info.patientIdcard" label="身份证" />
          <el-table-column align="center" prop="user_drug_Info.patientBirthday" label="出生日期" />
          <el-table-column align="center" prop="user_drug_Info.patientAddress" label="地址" />
          <el-table-column align="center" prop="checkDoctorName" label="初审医师" />
          <el-table-column align="center" prop="reviewDoctorName" label="复审医师" />
        </el-table>
        <el-table :data="[orderDetailList.prescription_info]" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                  border style="width: 100%;margin-top: 10px;">
          <el-table-column align="center" prop="status" label="是否有处方单">
            <template slot-scope="scope">
              <div v-if="scope.row.has_prescription==0">否</div>
              <div v-if="scope.row.has_prescription==1">是</div>
            </template>
          </el-table-column>
          <el-table-column v-if="orderDetailList.prescription_info.has_prescription==1" align="center" prop="status" label="处方单">
            <template slot-scope="scope">
              <el-image  v-for="(item,index) in scope.row.prescription_img_list" :key="index" :src="item.img_url" alt="" style="width: 60px;height: 60px;margin-right: 8px;margin-bottom: 10px"></el-image>
            </template>
          </el-table-column>
          <el-table-column v-if="orderDetailList.prescription_info.has_prescription==0" align="center" prop="status" label="审核状态"></el-table-column>
          <el-table-column v-if="orderDetailList.prescription_info.has_prescription==0" align="center" label="疾病名称">
            <template slot-scope="scope">
              <div>{{scope.row.prescription_disease_list}}</div>
            </template>
          </el-table-column>
          <el-table-column v-if="orderDetailList.prescription_info.has_prescription==0" align="center" prop="diagnoseDescription" label="患者主述/健康信息" />
        </el-table>
      </div>
    </div>-->

    <!-- secondStart-->
    <div class="secondConten">
      <div class="contenBottem ">
        <div class="title" style="margin-bottom: 20px;">子订单信息</div>
        <div class="detailAll" :style="'justify-content:'+(number==0?'space-around':'flex-start')">
          <el-table :data="order_item_list" :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            border style="width: 100%;">
            <el-table-column align="center" prop="id" label="序号" />
            <el-table-column align="center" prop="item_num" label="子订单号" />
            <el-table-column align="center" prop="goods_info.name" label="图片" >
              <template slot-scope="scope">
                  <div><img width="50%" :src="thumb_url[0].img_Url"/></div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="goods_info.upc" label="商家编号" />
            <el-table-column align="center" prop="goods_info.name" label="商家名称" />
            <el-table-column align="center" prop="goods_info.specification" label="规格" />
            <!-- <el-table-column align="center" prop="item_num" label="规格编码" /> -->
            <el-table-column align="center" prop="goods_info.classify_name" label="商家分类" />
            <el-table-column align="center" prop="discount_price" label="单价" />
            <el-table-column align="center" prop="num" label="数量" />
            <el-table-column align="center" prop="discount_amount" label="优惠信息">
               <template slot-scope="scope">
                 <!--<div v-if="scope.row.discount_amount==0">无</div>
                 <div v-else>{{scope.row.discount_amount}}</div>-->
                 <div>{{orderDetailList.coupon_name?orderDetailList.coupon_name:'无'}}</div>
               </template>
            </el-table-column>
            <el-table-column align="center" prop="total_amount" label="总价" />
          </el-table>

          <!-- v-for end-->
        </div>
        <div style="text-align: right;padding: 20px;color: #000000;">实付款：<span style="color: red;">￥{{orderDetailList.pay_amount}}</span></div>
        <div style="display: flex;justify-content: flex-end;color: #b9afaf;font-size: 15px;">
            <div style="margin-right: 20px;">含邮费：{{orderDetailList.delivery_fee}}</div>
            <div style="">优惠金额：{{orderDetailList.coupon_reduce_price}}</div>
        </div>
      </div>
    </div>

    <!-- 退款模态款 -->
    <el-dialog title="退款" style="margin-top: 16vh !important;" :visible.sync="refundDialog">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form ref="form" label-width="160px">
            <el-form-item label="退款金额">
              <el-input type="number" v-model="refund.refund_amount"></el-input>
            </el-form-item>
            <el-form-item label="退款原因">
              <el-input type="textarea" v-model="refund.reason" placeholder="退款原因，选填"></el-input>
            </el-form-item>
          </el-form>
          <div v-if="isShowRefundTip" style="text-align: center;color: red;padding-bottom: 15px">全额退款即视为取消订单</div>

          <el-button type="success" :loading="refundBtnLoading" @click="orderRefund()">退款</el-button>
          <el-button v-if="refundType==='apply'" type="danger" @click="orderRefund('no')">不同意</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 取消美团配送框 -->
    <el-dialog title="取消配送订单" style="margin-top: 16vh !important;" :visible.sync="cancelDeliveryOrderDialog">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form ref="form" label-width="160px">
            <el-form-item label="取消原因类别">
              <el-select style="width: 100%" v-model="cancelMeituanOrderParams.cancel_reason_id">
                <el-option v-for="(item,index) in MtCancelReason" :key="index" :value="item.code" :label="item.desc"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="详细取消原因">
              <el-input max="256" :rows="5" type="textarea" v-model="cancelMeituanOrderParams.cancel_reason" placeholder="详细取消原因"></el-input>
            </el-form-item>
          </el-form>


          <el-button type="primary" @click="cancelMeituanOrder">确定</el-button>
        </div>
      </div>
    </el-dialog>

    <!-- 添加模态款 -->
    <el-dialog title="设置发货信息" style="margin-top: 16vh !important;" :close-on-click-modal="false" :visible.sync="modelMsg">
      <div class="msgDiv">
        <!-- <h3>店铺基础信息</h3> -->
        <div>
          <el-form label-width="160px">
            <el-form-item label="订单序号">
              <el-input v-model="expressList.order_id" disabled></el-input>
            </el-form-item>
            <el-form-item label="快递公司/配送平台" :required="true">
              <el-input v-model="expressList.express" placeholder="请填写快递公司/配送平台"></el-input>
            </el-form-item>
            <el-form-item label="快递单号/配送单号" :required="true">
              <el-input v-model="expressList.express_num" placeholder="请填写快递单号/配送单号"></el-input>
            </el-form-item>

          </el-form>
          <div style="text-align: center;">
            <el-button type="success" @click="submitGoods(1)">直接发货</el-button>
            <el-button type="infor" @click="submitGoods(0)">保存草稿</el-button>
          </div>
        </div>
      </div>
    </el-dialog>

    <!-- 添加模态款 -->
    <el-dialog title="人工处理" :visible.sync="manualDialog">
      <image-drag
        :picList="prescriptionImg"
        :multiple="true"
        :tipShow="false"
        :action="()=> {return this.$api.UPLOAD_IMG}"
        :on-error="uploadError"
        :on-success="handleSuccess"
        @update="updateFile">
      </image-drag>
      <el-button style="margin-top: 20px" type="primary" @click="setOrderPrescriptionInfo">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
  import ElImageViewer from "element-ui/packages/image/src/image-viewer"
  import imageDrag from "../../shopManage/compoents/image-drag"
  import {CustomerReason} from "@/model/MtCancelReason";
  export default {
    name: "detail",
    components: { ElImageViewer,'image-drag': imageDrag },
    data() {
      return {
        order_id: "",
        orderDetailList: [],
        address_info: [],
        user_info: [],
        order_item_list: [],
        goods_info: [],
        thumb_url:[],
        number:"",
        totalSale:"",
        expressList: {
          order_id: "",
          express: "",
          express_num: "",
          is_shop: ""
        },
        modelMsg: false,
        refundDialog: false,
        refund: {
          order_id: '',
          refund_amount: '',
          reason: ''
        },
        refundType: '',
        refundBtnLoading: false,
        isShowRefundTip: false,
        prescriptionInfo: '',//处方信息
        showViewer: false,
        prescription_img_list: [],
        manualDialog: false,
        prescriptionImg: [],
        orderTransportInfo: '',
        orderTransportStep: '',
        cancelDeliveryOrderDialog: false,
        cancelMeituanOrderParams: {
          delivery_id: '',
          mt_peisong_id: '',
          cancel_reason_id: '',
          cancel_reason: '',
        },
        MtCancelReason: CustomerReason,
        checkIsChangeFlag: ''
      }

    },
    created() {
      this.order_id = this.$route.query.row
    },
    beforeDestroy() {
      this.checkIsChangeFlag = 'stop'
    },
    mounted() {
      this.getOrderDetail()
    },
    watch: {
      //监听相同路由下参数变化的时候，从而实现异步刷新
      $route(to, from) {
        this.order_id = this.$route.query.row;
        this.getOrderDetail()
      }
    },
    methods: {

      //骑手配送
      async riderDistribution(id) {
        if(!id){
          return
        }
        await this.$confirm('是否确认配送?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '关闭',
          type: 'warning'
        }).then(() => {
          this.$api.sendToMeituan({order_id: id}).then(res => {
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getOrderDetail()
            } else {

            }
          })

        }).catch(() => {

        });
      },

      //美团订单取消
      async cancelMeituanOrder() {
        if(!this.cancelMeituanOrderParams.cancel_reason_id){
          this.$message.error("取消原因类别")
          return
        }
        if(!this.cancelMeituanOrderParams.cancel_reason){
          this.$message.error("请填写取消原因")
          return
        }
        this.cancelMeituanOrderParams.mt_peisong_id = this.orderTransportInfo.mt_peisong_id
        const res = await this.$api.cancelMeituanOrder(this.cancelMeituanOrderParams)
        if(res.data.response.msg==="成功"){
          this.$message.success(res.data.response.msg)
          this.cancelDeliveryOrderDialog = false

          //取消订单存在延迟，延迟1秒再刷新数据
          await this.checkIsChangeStatus()

        } else {
          this.$message.error(res.data.response.msg)
        }
      },

      //递归，判断订单订单是否已经刷新状态了
      async checkIsChangeStatus() {
        //销毁当前页面要停止，防止会一直执行
        if(this.checkIsChangeFlag){
          return
        }
        await this.checkOrderStatus()
        if (this.orderTransportInfo.status == 99) {
          this.getOrderDetail()
          return true
        } else {
          setTimeout(() => {
            this.checkIsChangeStatus()
          }, 1000)
        }
      },

      //获取骑手配送的订单状态
      async checkOrderStatus() {
        const res = await this.$api.checkOrderStatus({order_id: this.order_id})
        if(res.data.level=="success"){
          if(!res.data.data){
            return
          }
          this.orderTransportInfo = res.data.data
          //判断配送步骤的数组
          if(this.orderTransportInfo.status==20){
            this.orderTransportStep = 2
          } else if(this.orderTransportInfo.status==30){
            this.orderTransportStep = 3
          } else if(this.orderTransportInfo.status==50){
            this.orderTransportStep = 4
          } else if(this.orderTransportInfo.status==99){
            /*this.orderTransportStep = 5*/
          } else{
            this.orderTransportStep = 1
          }
        }
      },

      //自提确认收货
     async confirmReceive(orderid){
        let params={
          order_id:orderid
        }
        await this.$confirm('确定要确认收货吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '关闭',
          type: 'success'
        }).then(() => {
          const loadingOne = this.$loading({
            lock: true,
            text: '取消中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$api.confirmReceive(params).then(res=>{
            if(res.data.level=="success"){
              this.$message.success(res.data.message)
              this.getOrderDetail()
              loadingOne.close()
            }else{
              this.$message.error(res.data.message)
              loadingOne.close()
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已关闭'
          });
        });

      },
      //直接发货
      submitGoods(is_shop) {
        /* is_shop 为0是保存草稿 , 为1直接发货 */
        this.expressList.is_shop = is_shop
        const loadingOne = this.$loading({
          lock: true,
          text: '保存中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        this.$api.setOrderShipInfo(this.expressList).then(res => {
          this.$message({
            type: res.data.level,
            message: res.data.message
          });
          if (res.data.level == "success") {
            window.location.reload()
            this.modelMsg = false
            loadingOne.close()
          } else {
            loadingOne.close()
            this.modelMsg = true
          }

        })
      },
      //打开弹窗
      deliverGoods(row) {
        this.expressList.order_id = row.id
        this.modelMsg = true
        this.expressList.express = row.express
        this.expressList.express_num = row.express_num
        this.expressList.is_shop =""
      },
      //取消订单
      async cancelOrder(id) {
        let params = {
          "order_id": id
        }
        await this.$confirm('确定要取消此订单吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '关闭',
          type: 'warning'
        }).then(() => {
          const loadingOne = this.$loading({
            lock: true,
            text: '取消中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$api.cancelOrder(params).then(res => {
            this.$message({
              type: res.data.level,
              message: res.data.message
            });
            if (res.data.level == "success") {
              this.getOrderDetail()
              loadingOne.close()
            } else {
              loadingOne.close()
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已关闭'
          });
        });

      },
      getOrderDetail() {

        this.$api.getOrderDetail({
          order_id: this.order_id
        }).then(res => {
          this.orderDetailList = res.data.data
          this.totalSale = this.orderDetailList.original_price-this.orderDetailList.discount_price
          this.address_info = this.orderDetailList.address_info
          this.user_info = this.orderDetailList.user_info
          this.order_item_list = this.orderDetailList.order_item_list
          this.order_item_list.forEach(item=>{
              this.number++
          })
          //计算子订单数量是奇数还是偶数
          this.number = this.number%2
          this.goods_info = this.order_item_list[0].goods_info
          this.thumb_url = this.goods_info.thumb_url

          //保存处方信息
          this.prescriptionInfo = this.orderDetailList.prescription_info?this.orderDetailList.prescription_info:{}

          //判断该订单是否为骑手配送，是则获取配送信息
          if(this.orderDetailList.delivery_method==='instant_delivery'){
            this.checkOrderStatus()
          }

        })
      },

      showRefundDialog(item,type) {
        let id = item.id
        let money = item.pay_amount
        let status = item.status
        if ([2, 3].includes(status)) {
          this.isShowRefundTip = true
        } else {
          this.isShowRefundTip = false
        }
        this.refundBtnLoading = false;
        this.refundDialog = true;
        this.refund.order_id = id;
        this.refund.reason = ''
        if(money){
          this.refund.refund_amount = money
        } else{
          this.refund.refund_amount = ''
        }
        this.refundType = type
      },

      //退款
      orderRefund(type) {
        if(type==="no"){
          this.refund.refund_amount = ''
        } else{
          if(!this.refund.refund_amount){
            this.$message.error("请输入退款金额")
            return
          } else if(this.refund.refund_amount==0){
            this.$message.error("退款金额要大于0")
            return
          }
        }
        this.refundBtnLoading = true;
        this.$api.orderRefund({
          order_id: this.refund.order_id,
          refund_amount: this.refund.refund_amount,
          reason: this.refund.reason
        }).then(res => {
          this.refundBtnLoading = false;
          this.$message({
            type: res.data.level,
            message: res.data.message
          });
          if (res.data.level == "success") {
            this.$message.success(res.data.message)
            this.getOrderList()
            this.refundDialog = false
          } else {
            if(res.data.message=="已拒绝"){
              this.getOrderList()
              this.refundDialog = false
            } else{
              this.$message.error(res.data.message)
            }
          }

        })
      },

      checkPrescriptionImg(images) {
       if(images){
         images.forEach(item => {
           this.prescription_img_list.push(item.img_url)
         })
       }
       this.showViewer = true
      },

      closeImgViewer() {
       this.showViewer = false
      },

      showManualDialog() {
        this.manualDialog = true
      },

      handleSuccess(res, file){
        this.prescriptionImg.push({id: res.data.id,key: file.uid,url: res.data.img_url})
      },
      //上传失败提示
      uploadError() {
        this.$message.error("上传失败，请重新上传");
      },
      updateFile(val) {
        this.prescriptionImg = val
      },

      async setOrderPrescriptionInfo() {
       let imgs = []
        this.prescriptionImg.forEach(item => {
          imgs.push(item.id)
        })
        const res = await this.$api.setOrderPrescriptionInfo({
          prescription_info_id: this.orderDetailList.prescription_info.id,
          prescription_imgs: imgs
        })
        if(res.data.level==="success"){
          this.$message.success(res.data.message)
          this.manualDialog = false
          this.getOrderDetail()
        } else {
          this.$message.error(res.data.message)
        }
      }

    }
  }
</script>
<style scoped lang="scss">
  @import "../../../assets/css/detailCss.scss";
  .msgDiv {
    width: 80%;
    margin: auto;
    text-align: center;
  }


  .prescription-status{
    padding: 15px 0;
    .left{
      font-weight: bold;
    }
    .right{
      margin-left: 20px;
    }
  }

  .prescription-info{
    color: #333333;
    .item-left,.item-right{
      border: 1px solid #EDEDED;
      padding: 0 5px;
      /*line-height: 50px;*/
      /*height: 50px;*/
      min-height: 50px;
      box-sizing: border-box;
      vertical-align: center;
      display: flex;
      align-items: center;
      font-size: 14px;
    }
    .item-left{
      /*font-weight: bold;*/
    }
    .item-right{

    }
  }

</style>
<style scoped>
  /* 图片上传css */
  .avatar-uploader /deep/.el-upload {
    border: 1px dashed #807979;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }

  /deep/.el-step__head.is-process{
    color: #67C23A;
    border-color: #67C23A;
  }

  /deep/.el-step__title.is-process{
    color: #67C23A;
  }

  /deep/.el-step__head.is-success{
    color: grey;
    border-color: grey;
  }

  /deep/.el-step__title.is-success{
    color: grey;
  }

  /deep/.el-step__head.is-wait{
    color: grey;
    border-color: grey;
  }

  /deep/.el-step__title.is-wait{
    color: grey;
  }

</style>
